﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////


#include "GnufExplorerBlockSorter.h"
#include "GnufModelViewDefine.h"
#include "ElementId.h"
#include "GnufInfo.h"
#include "IDocument.h"
#include "IElementBasicInformation.h"
#include "IModelView.h"
#include "MathUtils.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gfam;
using namespace gnuf;
using namespace gcmp;

bool gfam::ModelViewBlockSorter::compareImpl(const TreeNode* left, const TreeNode* right)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(m_pDoc != nullptr, L"m_pDoc is nullptr", L"paramodel", L"2024-02-20");

    if (left!=nullptr && right!= nullptr)
    {
        ElementId lId(left->userDataAt(Gnuf_MODEL_VIEW_USER_DATA_ROLE).cast<Int64>());
        ElementId rId(right->userDataAt(Gnuf_MODEL_VIEW_USER_DATA_ROLE).cast<Int64>());

        if (lId.IsValid() && rId.IsValid())
        {
            gcmp::IModelView* pLeftModelView = quick_cast<gcmp::IModelView>(m_pDoc->GetElement(lId));
            gcmp::IModelView* pRightModelView = quick_cast<gcmp::IModelView>(m_pDoc->GetElement(rId));
            if (pLeftModelView != nullptr && pRightModelView != nullptr)
            {
                double lheight = pLeftModelView->GetOrigin().GetZ();
                double rheight = pRightModelView->GetOrigin().GetZ();
                if (m_sortByLevel && !MathUtils::IsEqual(lheight, rheight))
                {
                    return MathUtils::IsLessThan(lheight, rheight);
                }

                if (pLeftModelView->GetBasicInformation() && pRightModelView->GetBasicInformation())
                {
                    return pLeftModelView->GetBasicInformation()->GetName() > pRightModelView->GetBasicInformation()->GetName();
                }

                return true;
            }
        }
    }

    return true;
}

bool gfam::ElevationModelViewBlockSorter::compareImpl(const gnuf::TreeNode* left, const gnuf::TreeNode* right)
{
    if (left != nullptr && right != nullptr)
    {
        auto getSortIndicator = [](const gnuf::TreeNode* pNode) ->int {
            const gcmp::Any & data = pNode->userDataAt(Gnuf_ELEVATION_MODEL_VIEW_USER_DATA_ROLE);
            if (data.empty()) return -1;

            return data.cast<int>();
        };

        int leftIndicator = getSortIndicator(left);
        int rightIndicator = getSortIndicator(right);

        if (leftIndicator == -1 || rightIndicator == -1)
        {
            DBG_WARN(L"invalid sort indicator", L"paramodel", L"2024-02-20");
        }
        else if (leftIndicator == rightIndicator)
        {
            if (left->nodeInfo(0) && right->nodeInfo(0))
            {
                return left->nodeInfo(0)->caption() > right->nodeInfo(0)->caption();
            }
            
            return true;
        }
        else
        {
            return leftIndicator > rightIndicator;
        }
    }
    return true;
}
